package com.ln.adoms.action;

import com.ln.adoms.entity.DepartmentType;
import com.ln.adoms.service.DepartmentTypeService;
import com.ln.core.orm.PageData;
import com.ln.core.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/adoms/departmentType")
public class DepartmentTypeAction {

	@Resource
	private DepartmentTypeService departmentTypeService;

	@RequestMapping("/list")
	public ModelMap list(HttpServletRequest request) {
		PageData<DepartmentType> pageData = new PageData<DepartmentType>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = departmentTypeService.find(pageData);

		return new ModelMap(pageData);
	}

    @RequestMapping(value = "/repeat")
    @ResponseBody
    public boolean repeat(String name, Integer id) {
        try {
            name = name.trim();
            List<DepartmentType> departmentTypeList = departmentTypeService.findList("name", name);
            if (departmentTypeList != null && !departmentTypeList.isEmpty()) {
                if (id != 0) {
                    DepartmentType d = departmentTypeService.find(id);
                    if (!d.getName().equals(name)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

	@RequestMapping(value = "/new")
	public String addForm(Model model) {
		// DepartmentType departmentType = new DepartmentType();
		model.addAttribute(new DepartmentType());
		return "/adoms/departmentType/input";
	}

	@RequestMapping(value = "/edit/{id}")
	public String editForm(@PathVariable("id") Integer id, Model model) {
		DepartmentType departmentType = departmentTypeService.find(id);
		model.addAttribute(departmentType);
		return "/adoms/departmentType/input";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@Valid DepartmentType departmentType, BindingResult result) {
		if (result.hasErrors()) {
			return "/adoms/departmentType/input";
		} else {
			if (departmentType.isNew()) {
				departmentTypeService.save(departmentType);
			} else {
				departmentTypeService.update(departmentType);
			}
		}

		return "redirect:/adoms/departmentType/list";// 重定向
	}

	@RequestMapping(value = "/delete/{id}")
	public String delete(@PathVariable("id") Integer id) {
		departmentTypeService.delete(id);
		return "redirect:/adoms/departmentType/list";// 重定向
	}

	@RequestMapping(value = "/visible/{id}")
	public String visible(@PathVariable("id") Integer id) {
		departmentTypeService.visible(id);
		return "redirect:/adoms/departmentType/list";// 重定向
	}

	@RequestMapping(value = "/unVisible/{id}")
	public String unVisible(@PathVariable("id") Integer id) {
		departmentTypeService.unVisible(id);
		return "redirect:/adoms/departmentType/list";// 重定向
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
